package com.turboconnard.hardware;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.turboconnard.data.Library;
import com.turboconnard.display.Stage;
import com.turboconnard.events.AccelerometerEvent;
import com.turboconnard.events.Event;
import com.turboconnard.events.EventDispatcher;
import com.turboconnard.geom.Point3D;

public class Accelerometer extends EventDispatcher  implements SensorEventListener {
	private static Accelerometer _instance;
	
	public Accelerometer(Context context) {
		super();		
		SensorManager s = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
		s.registerListener( this,s.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_GAME);
	}
	public void onAccuracyChanged(Sensor sensor, int value){
		
	}
	/**
	 * 
	 */	
	public void onSensorChanged(SensorEvent se) {
		Event e = new Event(AccelerometerEvent.ON_CHANGE);
		if(Stage.getInstance().getOrientation() == 1)
			e.param = new Point3D(se.values[1],se.values[0],se.values[2]);
		else 
			e.param = new Point3D(-se.values[0],se.values[1],se.values[2]);
		dispatchEvent(e);
	}
	public static Accelerometer getInstance(){
		if(_instance == null )_instance = new Accelerometer( Library.getInstance().context );
		return _instance;		
	}
}
